home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / progbar.exe / READREZ.CPP < prev    next >
C/C++ Source or Header  |  1992-10-12  |  6KB  |  241 lines

  1. /*---------------------------------------------------------*/
  2. /*   Reads a previously streamed TProgressBar Resource     */
  3. /*---------------------------------------------------------*/
  4. #define Uses_TObject
  5. #define Uses_TKeys
  6. #define Uses_TApplication
  7. #define Uses_TEvent
  8. #define Uses_TRect
  9. #define Uses_TDialog
  10. #define Uses_TStaticText
  11. #define Uses_TButton
  12. #define Uses_TMenu
  13. #define Uses_TMenuBar
  14. #define Uses_TSubMenu
  15. #define Uses_TMenuItem
  16. #define Uses_TStatusLine
  17. #define Uses_TStatusItem
  18. #define Uses_TStatusDef
  19. #define Uses_TDeskTop
  20. #define Uses_MsgBox
  21. #define Uses_TStreamableClass
  22. #define Uses_TResourceCollection
  23. #define Uses_TResourceFile
  24.  
  25. #define Uses_fpstream
  26.  
  27. #include <tv.h>
  28. #include "tprogbar.h"
  29. __link(RWindow);
  30. __link(RGroup);
  31. __link(RDialog);
  32. __link(RFrame);
  33. __link(RResourceCollection);
  34. __link(RButton);
  35. __link(RCluster);
  36. __link(RProgressBar);
  37.  
  38. #include <string.h>
  39. #include <stdlib.h>
  40.  
  41. const char rezFileName[] = "MY.REZ";
  42.  
  43. const cmAboutCmd   = 100;  // User selected menu item 'About'
  44. const cmStatusCmd  = 101;  // User selected menu item 'Progress Bar'
  45.  
  46. //========================================================================
  47.  
  48. class TMyApplication : public TApplication
  49. {
  50. public:
  51.    TMyApplication();
  52.    static TMenuBar *initMenuBar(TRect);
  53.    void handleEvent(TEvent &);
  54. private:
  55.    void aboutDlg();
  56.    void statusDlg();
  57.    Boolean isCancel(TDialog *pd);
  58. };
  59.  
  60. //========================================================================
  61.  
  62. TMyApplication::TMyApplication() :
  63. TProgInit(&TApplication::initStatusLine,&TMyApplication::initMenuBar,
  64.      &TApplication::initDeskTop)
  65. {
  66. }
  67.  
  68. //========================================================================
  69.  
  70. TMenuBar *TMyApplication::initMenuBar(TRect bounds)
  71. {
  72.      bounds.b.y = bounds.a.y + 1;
  73.      return(new TMenuBar(bounds,
  74.                 new TMenu(
  75.                      *new TMenuItem("~A~bout",cmAboutCmd,kbAltA,hcNoContext,0,
  76.                           new TMenuItem("~P~rogress Bar",cmStatusCmd,kbAltL,hcNoContext,0)))));
  77. }
  78.  
  79. //========================================================================
  80.  
  81. void TMyApplication::handleEvent(TEvent &event)
  82. {
  83.      TApplication::handleEvent(event);
  84.  
  85.      if (event.what == evCommand)
  86.      {
  87.           switch (event.message.command)
  88.           {
  89.           case cmAboutCmd:
  90.           {
  91.                 aboutDlg();
  92.                 clearEvent(event);
  93.                 break;
  94.           }
  95.           case cmStatusCmd:
  96.           {
  97.                 statusDlg();
  98.                 clearEvent(event);
  99.                 break;
  100.           }
  101.           }
  102.      }
  103. }
  104.  
  105. //========================================================================
  106.  
  107. void TMyApplication::aboutDlg()
  108. {
  109.      TDialog *pd = new TDialog(TRect(0,0,35,12),"About");
  110.      if (pd)
  111.      {
  112.     pd->options |= ofCentered;
  113.     pd->insert(new TStaticText(TRect(1,2,34,7),
  114.         "\003Turbo Vision Example\n\003\n"
  115.                      "\003Streaming a Progress Bar\n\003\n"));
  116.           pd->insert(new TButton(TRect(3,9,32,11),"~O~k",cmOK,bfDefault));
  117.  
  118.     if (validView(pd) != 0)
  119.     {
  120.                 deskTop->execView(pd);
  121.  
  122.         destroy(pd);
  123.     }
  124.      }
  125. }
  126.  
  127. //========================================================================
  128.  
  129. Boolean TMyApplication::isCancel(TDialog *pd)
  130. {
  131.    TEvent event;
  132.    pd->getEvent(event);
  133.    pd->handleEvent(event);
  134.    if(event.what==evCommand && event.message.command==cmCancel)
  135.       return (messageBox("Are you sure you want to Cancel",mfConfirmation|mfYesButton|mfNoButton)==cmYes ? True : False);
  136.    else
  137.       return False;
  138. }
  139.  
  140. void TMyApplication::statusDlg()
  141. {
  142.    // Read a progress bar from a stream.
  143.    TProgressBar *pbar;
  144.    TDialog *pd;
  145.    fpstream *ofps=0;
  146.    ofps = new fpstream( rezFileName, ios::in|ios::binary );
  147.    if( !ofps->good() )
  148.      messageBox("Stream open error", mfError|mfOKButton );
  149.  
  150.    TResourceFile *myRez;
  151.    myRez = new TResourceFile( ofps );
  152.  
  153.    if( !myRez )
  154.      messageBox("Resource file error", mfError|mfOKButton );
  155.    else
  156.      pbar = (TProgressBar*)myRez->get("theProgressBar");
  157.  
  158.    if( !pbar )// if "getting" resource failed...
  159.      messageBox("Get resource error", mfError|mfOKButton );
  160.    else {
  161.  
  162.       // Create the dialog box.
  163.       pd = (TDialog*)myRez->get("theDialogBox");
  164.       pd->insert(pbar);
  165.       TProgram::deskTop->insert(pd);     // Modeless !!!!
  166.  
  167.       int i=0;
  168.       Boolean keepOnGoing=True;
  169.  
  170.       TRect r(5,5,pd->size.x-5,pd->size.y-5);
  171.       TStaticText *theMessage;
  172.  
  173.       // The first 3rd.
  174.       theMessage = new TStaticText( r,
  175.      "This is a MODELESS dialog box. You can drag this box around the desktop." );
  176.       pd->insert(theMessage);
  177.       for(;i<=100;i++) {
  178.      pbar->update(i);
  179.      idle();
  180.      if(isCancel(pd)) {
  181.         keepOnGoing=False;
  182.         break;
  183.         }
  184.      delay(50);
  185.      }
  186.       destroy(theMessage);
  187.  
  188.       if(keepOnGoing) {
  189.      // The second 3rd
  190.      theMessage = new TStaticText( r,
  191.         "Notice that only the attribute is changed to show progress" );
  192.      pd->insert(theMessage);
  193.      for(;i<=200;i++) {
  194.         pbar->update(i);
  195.         idle();
  196.         if(isCancel(pd)) {
  197.            keepOnGoing=False;
  198.            break;
  199.            }
  200.         delay(50);
  201.         }
  202.      destroy(theMessage);
  203.      }
  204.  
  205.       if(keepOnGoing) {
  206.      // The last 3rd
  207.      theMessage = new TStaticText( r,
  208.         "Syntax: TProgressBar(TRect &r, double total, char bar);" );
  209.      pd->insert(theMessage);
  210.      for(;i<=300;i++) {
  211.         pbar->update(i);
  212.         idle();
  213.         if(isCancel(pd)) {
  214.            keepOnGoing=False;
  215.            break;
  216.            }
  217.         delay(50);
  218.         }
  219.      destroy(theMessage); // not necessary since we destroy
  220.                   // the TDialog also
  221.      }
  222.       destroy(pd);
  223.       }
  224.  
  225. }
  226.  
  227. //========================================================================
  228.  
  229. int main(void)
  230. {
  231.    TMyApplication myApplication;
  232.    myApplication.run();
  233.    return 0;
  234. }
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.